/* eslint react/no-string-refs:0 */
import React, { Component } from 'react';
import { Feedback} from '@icedesign/base';
import AuthForm from './AuthForm';
import { HashRouter as Router, Route, Link } from 'react-router-dom';
import API from '../../../../tools/API';
import JsonUtils from '../../../../tools/JsonUtils';
import {reload} from '../../../../menuConfig';

export default class LoginFrom extends Component {
  static displayName = 'LoginFrom';

  static defaultProps = {};

  formChange = (value) => {
    // console.log('formChange:', value);
  };

  handleSubmit = (errors, values) => {
    if (errors) {
      // console.log('errors', errors);
      return;
    }
    this.props.loading(true);
    const params = new Map();
    params.set("username",values.name);
    params.set("password",values.passwd);
    //调用接口登录
    API.getUserLogin(JsonUtils.mapToJson(params)).then((res) => {
      if(res.code != "000"){
        this.props.loading(false);
        Feedback.toast.error(res.msg);
      }else{
        //记住密码
        if(values.checkbox){   //是否保存密码
            let accountInfo = values.name+ ',' +values.passwd;
            let Days = 7;  //cookie保存时间
            let exp = new Date();
            exp.setTime(exp.getTime() + Days*24*60*60*1000);
            document.cookie = 'accountInfo' + "="+ escape (accountInfo) + ";expires=" + exp.toGMTString();
        }else {
            let exp = new Date();
            exp.setTime(exp.getTime() - 1);
            let accountInfo = document.cookie
            var cookie_pos = accountInfo.indexOf('accountInfo');
            if(cookie_pos!=-1){
                document.cookie= 'accountInfo' + "="+' '+";expires="+exp.toGMTString();
            }
        }
        // 登录成功后做对应的逻辑处理
        // location.replace("#/");
        reload();
        this.props.loading(false);
        Feedback.toast.success('登录成功');
        window.location.href="#/";
      }
    }).catch((err) => {
      this.props.loading(false);
      Feedback.toast.error('系统异常！');
    })
  };


  render() {
    const config = [
      {
        label: '用户名',
        component: 'Input',
        componentProps: {
          placeholder: '用户名',
          size: 'large',
          maxLength: 20,
        },
        formBinderProps: {
          name: 'name',
          required: true,
          message: '必填',
        },
      },
      {
        label: '密码',
        component: 'Input',
        componentProps: {
          placeholder: '密码',
          htmlType: 'password',
        },
        formBinderProps: {
          name: 'passwd',
          required: true,
          message: '必填',
        },
      },
      {
        label: '记住账号',
        component: 'Checkbox',
        componentProps: {},
        formBinderProps: {
          name: 'checkbox',
        },
      },
      {
        label: '登录',
        component: 'Button',
        componentProps: {
          type: 'primary',
        },
        formBinderProps: {},
      },
    ];



    const links = [
      { to: '/register', text: '立即注册' },
      { to: '/forgetpassword', text: '找回密码' },
    ];

    let initFields = {
      name : "",
      passwd : "",
      checkbox : false,
    }
    let arr,reg=new RegExp("(^| )"+'accountInfo'+"=([^;]*)(;|$)");
    let accountInfo =''
    if(arr=document.cookie.match(reg)){
        accountInfo =  unescape(arr[2]);
    }
    else{
        accountInfo = null;
    }
    if(Boolean(accountInfo) == true){
        let name = "";
        let passwd = "";
        let checkbox = "";

        let i=new Array()
        i = accountInfo.split(",");
        initFields.name = i[0],
        initFields.passwd = i[1],
        initFields.checkbox = true
    }
    return (
        <AuthForm
          title="登录"
          config={config}
          initFields={initFields}
          formChange={this.formChange}
          handleSubmit={this.handleSubmit}
          links={links}
        />
    );
  }
}
